#include "dialog.ch"
#include "inkey.ch"

//----------------------------------------------------------------------------//

CLASS CbMenu FROM Menu

   DATA oLabel
   DATA lHorizontal

   METHOD New        
   METHOD AddItem    
   METHOD Display    
   METHOD Hide       
   METHOD lIsOver    
   METHOD SetFocus   
   METHOD KeyPressed 
   METHOD Click      
   METHOD cGetHotKey 
   METHOD CbtChanged 
   METHOD SetValue   

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( nRow, nCol, cLabel, nId, cMessage, lHorizontal ) CLASS CbMenu

   DEFAULT nRow = 0, nCol = 0, cLabel = "&CbMenu", nId = 0, lHorizontal = .f.

   ::Parent:New()

   ::nTop            = nRow
   ::nLeft           = nCol
   ::nBottom         = nRow
   ::nRight          = nCol
   ::oLabel          = Label():New( -1, 0, cLabel )
   ::oLabel:cMessage = nil
   ::oLabel:oParent  = Self
   ::nId             = nId
   ::cMessage        = cMessage
   ::lHorizontal     = lHorizontal
   ::cClrNormal      = "N/BG"
   ::cClrFocus       = "W+/N"
   ::nOption         = 1

return Self

//----------------------------------------------------------------------------//

METHOD AddItem( cText ) CLASS CbMenu

   local oLastItem := ATail( ::aItems )
   local oNewItem  := CheckBtn():New( 0, 0, cText, 0 )
   local nMaxWidth := 0

   if ::lHorizontal
      oNewItem:Move( 0,;
                     If( oLastItem != nil,;
                     oLastItem:nLeft + nLabelLen( oLastItem:cText ) + 5, 0 ) )
      ::nRight += nLabelLen( oNewItem:cText )
   else
      oNewItem:Move( If( oLastItem != nil, oLastItem:nTop + 1, 0 ), 0 )
      ::nBottom++
      ::nRight = ::nLeft + Max( ::nRight - ::nLeft,;
                                nLabelLen( oNewItem:cText ) )
   endif

   oNewItem:cClrNormal = "N/BG"
   oNewItem:cClrFocus  = ::cClrFocus
   oNewItem:cClrHotKey = "GR+" + SubStr( oNewItem:cClrNormal, At( "/", oNewItem:cClrNormal ) )
   oNewItem:cMessage   = nil
   ::Parent:AddItem( oNewItem )

   if ! ::lHorizontal
      AEval( ::aItems,;
          { | oItem | nMaxWidth := Max( nMaxWidth, nLabelLen( oItem:cText ) ) } )
      AEval( ::aItems,;
          { | oItem | oItem:SetText( PadR( oItem:cText, nMaxWidth + ;
          If( At( "&", oItem:cText ) != 0, 1, 0 ) ) ) } )
   endif

return nil

//----------------------------------------------------------------------------//

METHOD Display() CLASS CbMenu

   if ::oLabel != nil
      ::oLabel:Show()
   endif
   ::Parent:Display()

return nil

//----------------------------------------------------------------------------//

METHOD Hide() CLASS CbMenu

   if ::oLabel != nil
      ::oLabel:Hide()
   endif
   ::Parent:Hide()

return nil

//----------------------------------------------------------------------------//

METHOD lIsOver( nRow, nCol ) CLASS CbMenu

   local lIsOver := If( ::oLabel != nil, ::oLabel:lIsOver( nRow, nCol ), .f. )

   if ! lIsOver
      lIsOver = ::Parent:lIsOver( nRow, nCol )
   endif

return lIsOver

//----------------------------------------------------------------------------//

METHOD SetFocus( lOnOff, cClrFocus ) CLASS CbMenu

   if ::oLabel != nil
      ::oLabel:SetFocus( lOnOff, cClrFocus )
   endif
   ::Parent:SetFocus( lOnOff, cClrFocus )

return nil

//----------------------------------------------------------------------------//

METHOD KeyPressed( nKey ) CLASS CbMenu

   do case
      case nKey == 32 .or. nKey == K_ENTER
           if ::nOption != 0
              ::aItems[ ::nOption ]:KeyPressed( nKey )
           endif

      otherwise
           ::Parent:KeyPressed( nKey )
   endcase

return nil

//----------------------------------------------------------------------------//

METHOD Click( nMRow, nMCol ) CLASS CbMenu

   ::Parent:Click( nMRow, nMCol )
   if ::nOption != 0
      if ::aItems[ ::nOption ]:lIsOver( nMRow(), nMCol() )
         ::aItems[ ::nOption ]:KeyPressed( 32 )
      endif
   endif

return nil

//----------------------------------------------------------------------------//

METHOD cGetHotKey() CLASS CbMenu

return If( ::oLabel != nil, ::oLabel:cGetHotKey(), "" )

//----------------------------------------------------------------------------//

METHOD CbtChanged( oCheckBtn ) CLASS CbMenu

   if ::oParent != nil
      ::oParent:CbmChanged( Self )
   endif

return nil

//----------------------------------------------------------------------------//

METHOD SetValue( nItem, lOnOff ) CLASS CbMenu

   ::aItems[ nItem ]:lChecked = lOnOff
   ::aItems[ nItem ]:Display()

return nil

//----------------------------------------------------------------------------//